package net.modul.customer.dao.impl;

import java.util.List;

import net.common.dao.GeneralDaoImpl;
import net.common.service.PageBean;
import net.modul.customer.dao.CustOrgDao;
import net.modul.customer.vo.CustOrgVo;

import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

public class CustOrgDaoImpl extends GeneralDaoImpl implements CustOrgDao {

	@Override
	public List<CustOrgVo> search(PageBean pageBean, CustOrgVo bean)
			throws Exception {
		String sql = "select org_id,cust_id,district_id,org_num,jane_spell,org_name,telphone,phone,linkman,email,address,flag,defined_1,defined_2,defined_3,defined_4,defined_5,defined_6,defined_7,defined_8,defined_9,defined_10,defined_12,defined_13,defined_15,defined_16,defined_17,defined_18,defined_19,defined_20 from t_cust_org where flag=0 and cust_id=?";
		if (bean.getOrg_num() != null && !bean.getOrg_num().equals("")) {
			sql += " and cust_name=?";
		}
		if (bean.getOrg_name() != null) {
			sql += " and org_name like '%?%'";
		}
		ResultSetHandler<List<CustOrgVo>> rsh = new BeanListHandler<CustOrgVo>(
				CustOrgVo.class);
		return gqr.querySplitPage(sql, rsh, bean, pageBean, null);
	}

	@Override
	public void insert(CustOrgVo bean) throws Exception {
		String sql = "insert into t_cust_org (cust_id,district_id,org_num,jane_spell,org_name,telphone,phone,linkman,email,address,flag,defined_1,defined_2,defined_3,defined_4,defined_5,defined_6,defined_7,defined_8,defined_9,defined_10,defined_12,defined_13,defined_15,defined_16,defined_17,defined_18,defined_19,defined_20) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		gqr.insert(sql, bean);
	}

	@Override
	public void update(CustOrgVo bean) throws Exception {
		String sql = "update t_cust_org set cust_id=?,district_id=?,org_num=?,jane_spell=?,org_name=?,telphone=?,phone=?,linkman=?,email=?,address=?,defined_1=?,defined_2=?,defined_3=?,defined_4=?,defined_5=?,defined_6=?,defined_7=?,defined_8=?,defined_9=?,defined_10=?,defined_12=?,defined_13=?,defined_15=?,defined_16=?,defined_17=?,defined_18=?,defined_19=?,defined_20=? where org_id=?";
		gqr.updateByBean(sql, bean);
	}

	@Override
	public void updateFlag(CustOrgVo bean) throws Exception {
		String sql = "update t_cust_org set flag=? where org_id=?";
		gqr.updateByBean(sql, bean);
	}

	@Override
	public CustOrgVo find(Integer id) throws Exception {
		String sql = "select org_id,cust_id,district_id,org_num,jane_spell,org_name,telphone,phone,linkman,email,address,flag,defined_1,defined_2,defined_3,defined_4,defined_5,defined_6,defined_7,defined_8,defined_9,defined_10,defined_12,defined_13,defined_15,defined_16,defined_17,defined_18,defined_19,defined_20 from t_cust_org where org_id=?";
		ResultSetHandler<CustOrgVo> rsh = new BeanHandler<CustOrgVo>(
				CustOrgVo.class);
		return gqr.query(sql, rsh, new Object[] { id });
	}
}
